<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运动" prop="sport">
        <el-select
          v-model="queryParams.sport"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.stu_habits_sport"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="健身" prop="fitnes">
        <el-select
          v-model="queryParams.fitnes"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.stu_habits_fitnes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="游戏" prop="game">
        <el-select
          v-model="queryParams.game"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.stu_habits_game"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="熬夜" prop="night">
        <el-select
          v-model="queryParams.night"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.stu_habits_night"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="早起" prop="bright">
        <el-select
          v-model="queryParams.bright"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.stu_habits_bright"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="看书" prop="reading">
        <el-select
          v-model="queryParams.reading"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.stu_habits_reading"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['students:habits:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['students:habits:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['students:habits:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['students:habits:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="habitsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="stuId" />
      <el-table-column label="运动" align="center" prop="sport" >
        <template slot-scope="scope">
          {{scope.row.sport===1?"是":"否"}}
        </template>
      </el-table-column>
      <el-table-column label="健身" align="center" prop="fitnes" >
      <template slot-scope="scope">
        {{scope.row.fitnes===1?"是":"否"}}
      </template>
      </el-table-column>
      <el-table-column label="游戏" align="center" prop="game" >
      <template slot-scope="scope">
        {{scope.row.game===1?"是":"否"}}
      </template>
      </el-table-column>
      <el-table-column label="熬夜" align="center" prop="night" >
      <template slot-scope="scope">
        {{scope.row.night===1?"是":"否"}}
      </template>
      </el-table-column>
      <el-table-column label="早起" align="center" prop="bright" >
        <template slot-scope="scope">
          {{scope.row.bright===1?"是":"否"}}
        </template>
      </el-table-column>
      <el-table-column label="看书" align="center" prop="reading" >
        <template slot-scope="scope">
          {{scope.row.reading===1?"是":"否"}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['students:habits:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['students:habits:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生习惯信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="运动" prop="sport">
          <el-select v-model="form.sport" placeholder="是否喜欢运动">
            <el-option
              v-for="dict in dict.type.stu_habits_sport"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="健身" prop="fitnes">
          <el-select v-model="form.fitnes" placeholder="是否喜欢健身">
            <el-option
              v-for="dict in dict.type.stu_habits_fitnes"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏" prop="game">
          <el-select v-model="form.game" placeholder="是否喜欢游戏">
            <el-option
              v-for="dict in dict.type.stu_habits_game"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="熬夜" prop="night">
          <el-select v-model="form.night" placeholder="是否喜欢熬夜">
            <el-option
              v-for="dict in dict.type.stu_habits_night"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="早起" prop="bright">
          <el-select v-model="form.bright" placeholder="是否喜欢早起">
            <el-option
              v-for="dict in dict.type.stu_habits_bright"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="看书" prop="reading">
          <el-select v-model="form.reading" placeholder="是否喜欢看书">
            <el-option
              v-for="dict in dict.type.stu_habits_reading"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHabits, getHabits, delHabits, addHabits, updateHabits } from "@/api/students/habits";

export default {
  name: "Habits",
  dicts: ['stu_habits_sport','stu_habits_fitnes','stu_habits_game','stu_habits_night','stu_habits_bright','stu_habits_reading'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生习惯信息表格数据
      habitsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sport: null,
        fitnes: null,
        game: null,
        night: null,
        bright: null,
        reading: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sport: [
          { required: true, message: "运动不能为空", trigger: "blur" }
        ],
        fitnes: [
          { required: true, message: "健身不能为空", trigger: "blur" }
        ],
        game: [
          { required: true, message: "游戏不能为空", trigger: "blur" }
        ],
        night: [
          { required: true, message: "熬夜不能为空", trigger: "blur" }
        ],
        bright: [
          { required: true, message: "早起不能为空", trigger: "blur" }
        ],
        reading: [
          { required: true, message: "看书不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生习惯信息列表 */
    getList() {
      this.loading = true;
      listHabits(this.queryParams).then(response => {
        this.habitsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stuId: null,
        sport: null,
        fitnes: null,
        game: null,
        night: null,
        bright: null,
        reading: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生习惯信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stuId = row.stuId || this.ids
      getHabits(stuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生习惯信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stuId != null) {
            updateHabits(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHabits(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stuIds = row.stuId || this.ids;
      this.$modal.confirm('是否确认删除学生习惯信息编号为"' + stuIds + '"的数据项？').then(function() {
        return delHabits(stuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('students/habits/export', {
        ...this.queryParams
      }, `habits_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
